/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.date.DateUtil
 *  cn.hutool.core.util.ObjectUtil
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.ComboBox
 *  javafx.scene.control.DatePicker
 *  javafx.scene.control.TextField
 *  javafx.scene.image.ImageView
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.Pane
 *  javafx.stage.Stage
 *  javafx.util.StringConverter
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.gys.common.data.KeyValue;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.salesReceipts.SpecialDrugsOutData;
import com.gys.view.salesReceipts.PdAndExpView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class SpecialDrugsController
extends BaseController
implements Initializable {
    @FXML
    private TextField address;
    @FXML
    private TextField phone;
    @FXML
    private TextField cardId;
    @FXML
    private ComboBox<KeyValue> sex;
    @FXML
    private TextField name;
    @FXML
    private Pane pane;
    @FXML
    private DatePicker birthDate;
    @FXML
    private ImageView faceImage;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    public boolean inputFlag = false;
    private Timer timer = new Timer();
    private int enterCount = 0;
    public SpecialDrugsOutData specialDrugsOutData;
    private String caseId = "";

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        ObservableList sexOptions = FXCollections.observableArrayList(new ArrayList());
        sexOptions.add(new KeyValue("\u7537", "1"));
        sexOptions.add(new KeyValue("\u5973", "0"));
        this.sex.setItems(sexOptions);
        CommonUtil.comboxInit(this.sex);
        StringConverter converter = CommonUtil.dataPickerFormat("yyyyMMdd");
        this.birthDate.setConverter(converter);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        if (!this.caseId.equals(this.salesReceiptsMainController.getCaseId().getText())) {
            this.initForm();
            this.caseId = this.salesReceiptsMainController.getCaseId().getText();
        }
        this.specialDrugsOutData = new SpecialDrugsOutData();
        this.enterCount = 0;
        this.timer.schedule(new TimerTask(){

            @Override
            public void run() {
                PdAndExpView view = SpringUtil.getBean(PdAndExpView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.F1) {
                            SpecialDrugsController.this.readAction();
                        } else if (e.getCode() == KeyCode.ENTER) {
                            if (SpecialDrugsController.this.enterCount == 0) {
                                SpecialDrugsController.this.sureAction();
                            } else if (SpecialDrugsController.this.enterCount == 1) {
                                SpecialDrugsController.this.readAction();
                            }
                        }
                        e.consume();
                    }
                });
            }
        }, 500L);
    }

    private void initForm() {
        this.address.clear();
        this.phone.clear();
        this.cardId.clear();
        this.name.clear();
        this.inputFlag = false;
        this.sex.setValue(new KeyValue());
        this.birthDate.setValue(null);
        this.faceImage.setImage(null);
    }

    @FXML
    private void sureAction() {
        if (!this.checkData()) {
            return;
        }
        this.specialDrugsOutData.setAddress(this.address.getText());
        this.specialDrugsOutData.setBirthDate(DateUtil.format((Date)DateUtil.parse((String)((LocalDate)this.birthDate.getValue()).toString()), (String)"yyyyMMdd"));
        this.specialDrugsOutData.setCardId(this.cardId.getText());
        this.specialDrugsOutData.setName(this.name.getText());
        this.specialDrugsOutData.setPhone(this.phone.getText());
        this.specialDrugsOutData.setSex(((KeyValue)this.sex.getValue()).getValue());
        this.inputFlag = true;
        this.cancelAction();
    }

    private boolean checkData() {
        String message = "";
        if (ObjectUtil.isEmpty(this.cardId.getText()) || !CommonUtil.isIDNumber(this.cardId.getText())) {
            message = "\u8eab\u4efd\u8bc1\u53f7\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u6216\u683c\u5f0f\u9519\u8bef";
        } else if (ObjectUtil.isEmpty(this.name.getText())) {
            message = "\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (ObjectUtil.isEmpty(((KeyValue)this.sex.getValue()).getValue())) {
            message = "\u6027\u522b\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (ObjectUtil.isEmpty(this.birthDate.getValue())) {
            message = "\u51fa\u751f\u5e74\u6708\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (ObjectUtil.isEmpty(this.phone.getText()) || !CommonUtil.checkPhone(this.phone.getText())) {
            message = "\u624b\u673a/\u7535\u8bdd\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u6216\u683c\u5f0f\u9519\u8bef";
        } else if (this.name.getText().length() > 20) {
            message = "\u59d3\u540d\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc710";
        } else if (this.address.getText().length() > 100) {
            message = "\u5730\u5740\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc7100";
        }
        if (ObjectUtil.isNotEmpty(message)) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", message);
            alert.showAndWait();
            return false;
        }
        return true;
    }

    @FXML
    private void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }

    @FXML
    private void readAction() {
        this.enterCount = 1;
    }
}

